
package fregat.base.util;

/**
 * Functional-class to view a string contens in diverses notations ( decimal,hex,octal,binary ).
 *
 * @author Kovyrov Vadim <br><b><i>ECO_L O G</i> &nbsp Logistiksysteme GmbH</b>
 * @version 1.0
 */
public class ViewString {
    private static void byteAsString( StringBuffer sb, byte b, int radix ){
        switch(radix){
            case 2:
                String s = Integer.toBinaryString( b );
                sb.append(' ');
                for(int i=0; i+s.length()<8; i++ ){sb.append('0');}
                sb.append( s.substring( ((s.length()- 8)>0)?(s.length()-8):(0)) );
                break;
        	case 10:
        	    sb.append(' '); sb.append( Integer.toString(b)); break;
        	case 16:
        	    s = Integer.toHexString( b );
        	    sb.append(' '); 
        	    for(int i=0; i+s.length()<2; i++ ){sb.append('0');}
                sb.append( s.substring( ((s.length()- 2)>0)?(s.length()-2):(0)) ); break;
        	case 8:
        	    s = Integer.toOctalString( b );
        	    sb.append(' '); 
        	    for(int i=0; i+s.length()<3; i++ ){sb.append('0');}
                sb.append( s.substring( ((s.length()- 3)>0)?(s.length()-3):(0)) ); break;
        	default: break;   
        	}
        }
    public static String displayStringAsByteArray( String s, int radix ){
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i<s.length(); i++){
            byteAsString(sb, s.getBytes()[i], radix );
            }
        return new String(sb);
        }
    
    public static void main(String[] args) {
        byte[] fr_telegram = new byte[]{'w','e','r','2','0','0','1','R','B','0','1',-2,0x7e,
                'F','R','O','K',0x0,0x0,0x0,0xd,0x0,0x2,0x0,0xc,0x0,0x2,'A','2','3','5'};
        String toConv = new String( fr_telegram );
        
        System.out.println( "String = "+ViewString.displayStringAsByteArray(toConv, 16 ));
    }
}
